//
//  Custom_PlaceholderTextView.m
//  WEIXUE_RN
//
//  Created by WangGuoli on 2017/12/8.
//  Copyright © 2017年 zhanghao. All rights reserved.
//

#import "Custom_PlaceholderTextView.h"
@interface Custom_PlaceholderTextView ()
@property (nonatomic,strong) UILabel *placeholderLabel;
/**  数字  */
@property (nonatomic, strong) UILabel * numLabel;

@end
@implementation Custom_PlaceholderTextView

- (instancetype)initWithFrame:(CGRect)frame{
  self = [super initWithFrame:frame];
  if(self) {
    self.backgroundColor= [UIColor clearColor];
    UILabel *placeholderLabel = [[UILabel alloc]init];//添加一个占位label
    placeholderLabel.backgroundColor= [UIColor clearColor];
    placeholderLabel.numberOfLines=0; //设置可以输入多行文字时可以自动换行
    [self addSubview:placeholderLabel];
    self.placeholderLabel = placeholderLabel; //赋值保存
    self.myPlaceholderColor= [UIColor lightGrayColor]; //设置占位文字默认颜色
    self.font= [UIFont systemFontOfSize:15]; //设置默认的字体
    
    
    _numLabel = [Custom_ZHTool uiLabelCreate:@"" Font:FONT(RATIO(20)) TextColor:[ColorUtil colorWithHexadecimal:@"#999999"]];
    _numLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_numLabel];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self]; //通知:监听文字的改变
    

  }
  return self;
}

#pragma mark -监听文字改变

- (void)textDidChange{
  
  if (self.limitNum > 0) {
    self.numLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.text.length,(long)self.limitNum];
      if (self.text.length >= self.limitNum) {
          self.numLabel.textColor = [UIColor redColor];
      }else{
        self.numLabel.textColor = [ColorUtil colorWithHexadecimal:@"#999999"];
      }
      
  }
  self.placeholderLabel.hidden = self.hasText;
}
- (void)layoutSubviews{
  [super layoutSubviews];
  self.placeholderLabel.MM_y=25; //设置UILabel 的 y值
  self.placeholderLabel.MM_x=5+self.placeLeftDistance; //设置 UILabel 的 x 值
  self.placeholderLabel.MM_w=self.MM_w-self.placeholderLabel.MM_x * 2.0; //设置 UILabel 的 x
  //根据文字计算高度
  CGSize maxSize = CGSizeMake(self.placeholderLabel.MM_w, MAXFLOAT);
  self.placeholderLabel.MM_h = [self.myPlaceholder boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.placeholderLabel.font} context:nil].size.height;
  
  //底部字数限制
  
  NSLog(@"self.contentSize.height=%f",self.contentSize.height);
  
  CGFloat numLabelY = self.MM_h - 50;
  
  if (numLabelY < (self.contentSize.height - 50)) {
    numLabelY = self.contentSize.height - 50;
  }
  self.numLabel.MM_x = 0;
  self.numLabel.MM_y = numLabelY;
  self.numLabel.MM_w = self.MM_w - 20;
  self.numLabel.MM_h = 30;
  
}

- (void)setMyPlaceholder:(NSString*)myPlaceholder{
  
  _myPlaceholder= [myPlaceholder copy];
  
  //设置文字
  
  self.placeholderLabel.text= myPlaceholder;
  
  //重新计算子控件frame
  
  [self setNeedsLayout];
  
}
- (void)setMyPlaceholderColor:(UIColor*)myPlaceholderColor{
  _myPlaceholderColor= myPlaceholderColor;
  //设置颜色
  self.placeholderLabel.textColor= myPlaceholderColor;
}

- (void)setPlaceLeftDistance:(CGFloat)placeLeftDistance {
    _placeLeftDistance = placeLeftDistance;
    
    [self setNeedsLayout];
}

//重写这个set方法保持font一致

- (void)setFont:(UIFont*)font{
  [super setFont:font];
  self.placeholderLabel.font= font;
  //重新计算子控件frame
  [self setNeedsLayout];
}

- (void)setLimitNum:(NSInteger)limitNum{
    if (limitNum <= 0) {
        self.numLabel.text = @"";
        self.textContainerInset = UIEdgeInsetsMake(30, 0, 50, 0);
        return;
    }
  _limitNum = limitNum;
  
  self.numLabel.text = [NSString stringWithFormat:@"%ld/%ld",self.text.length,(long)limitNum];
  //设置UITextView的内边距
//    self.textContainerInset = UIEdgeInsetsMake(0, 0, 50, 30);
  
  [self setNeedsLayout];
  
}
- (void)setText:(NSString*)text{
  [super setText:text];
  [self textDidChange]; //这里调用的就是 UITextViewTextDidChangeNotification 通知的回调
}
- (void)setAttributedText:(NSAttributedString*)attributedText{
  [super setAttributedText:attributedText];
  [self textDidChange]; //这里调用的就是UITextViewTextDidChangeNotification 通知的回调
}

- (void)dealloc{
  [[NSNotificationCenter defaultCenter] removeObserver:UITextViewTextDidChangeNotification];
}

@end
